package com.runfast.waimai.service.impl;

import com.runfast.common.service.BaseService;
import com.runfast.waimai.dao.mapper.WaiMaiRunfastAgentbusinessMapper;
import com.runfast.waimai.dao.model.WaiMaiRunfastAgentbusiness;
import com.runfast.waimai.dao.model.WaiMaiRunfastAgentbusinessExample;
import com.runfast.waimai.service.WaiMaiRunfastAgentbusinessService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class WaiMaiRunfastAgentbusinessServiceImpl extends BaseService<WaiMaiRunfastAgentbusiness, Integer, WaiMaiRunfastAgentbusinessExample> implements WaiMaiRunfastAgentbusinessService {
    /**根据代理商的id查询代理商*/
    public WaiMaiRunfastAgentbusiness setAgent(Integer agentId){
        WaiMaiRunfastAgentbusiness  agentbusiness=new WaiMaiRunfastAgentbusiness();
        if(agentId!=null){
            agentbusiness=this.selectByPrimaryKey(agentId);
        }
        return agentbusiness;
    }

    public List<Integer> findByAgentId(Integer agentId) {
        List<Integer> classes = new ArrayList<Integer>();
        List<WaiMaiRunfastAgentbusiness> agents = new ArrayList<WaiMaiRunfastAgentbusiness>();
        classes.add(agentId);
        WaiMaiRunfastAgentbusiness rootType = this.selectByPrimaryKey(agentId);//顶级区域
        if(rootType != null){
            List<WaiMaiRunfastAgentbusiness> allClass = this.selectByExample(null);
            if(allClass != null){
                for(WaiMaiRunfastAgentbusiness curClass: allClass){
                    if(curClass.getAgentid() != null && curClass.getAgentid().equals(rootType.getId())){//下级区域
                        agents.add(curClass);
                        getSubClass(curClass,allClass,agents);
                    }
                }
            }
        }
        for(WaiMaiRunfastAgentbusiness a:agents){
            if(a!=null){
                classes.add(a.getId());
            }
        }
        return classes;
    }
    private void getSubClass(WaiMaiRunfastAgentbusiness curClass, List<WaiMaiRunfastAgentbusiness> allClass,
                             List<WaiMaiRunfastAgentbusiness> classes) {
        for(WaiMaiRunfastAgentbusiness curr: allClass){
            if(curr.getAgentid() != null &&curr.getAgentid().equals(curClass.getId())){//抽取自由人的下级
                classes.add(curr);
                this.getSubClass(curr,allClass,classes);
            }
        }
    }


   public Integer countAgentByExample(Map<Object,Object> map1){
       WaiMaiRunfastAgentbusinessMapper waiMaiRunfastAgentbusinessMapper=(WaiMaiRunfastAgentbusinessMapper)this.mapper;
       return waiMaiRunfastAgentbusinessMapper.countAgentByExample(map1);
    }
    public List<WaiMaiRunfastAgentbusiness> selectAgentByExampleWithPageable(Map<Object,Object> map1){
        WaiMaiRunfastAgentbusinessMapper waiMaiRunfastAgentbusinessMapper=(WaiMaiRunfastAgentbusinessMapper)this.mapper;
        return waiMaiRunfastAgentbusinessMapper.selectAgentByExampleWithPageable(map1);
    }

    public List<WaiMaiRunfastAgentbusiness> selectAgentStatisticsByMapWithPageable(Map<Object,Object> map){
        WaiMaiRunfastAgentbusinessMapper waiMaiRunfastAgentbusinessMapper=(WaiMaiRunfastAgentbusinessMapper)this.mapper;
        return waiMaiRunfastAgentbusinessMapper.selectAgentStatisticsByMapWithPageable(map);
    }
    public Integer countAgentStatisticsByMap(Map<Object,Object> map){
        WaiMaiRunfastAgentbusinessMapper waiMaiRunfastAgentbusinessMapper=(WaiMaiRunfastAgentbusinessMapper)this.mapper;
        return waiMaiRunfastAgentbusinessMapper.countAgentStatisticsByMap(map);
    }

}